import { Args, Query, Resolver, ResolveField, Parent } from '@nestjs/graphql';
import { AuthorsService } from '../services/authors.service';
import { PostsService } from '../services/posts.service';
import { Author, Post } from '../models/models';
import { AuthorIdArg } from '../dto/args';

@Resolver(of => Author)
export class AuthorsResolver {
    constructor(
        private authorsService: AuthorsService,
        private postsService: PostsService,
    ) { }

    @Query(returns => Author, { name: 'author' })
    async getAuthor(@Args() args: AuthorIdArg) {
        const author = await this.authorsService.findAuthorById(args.id);
        return author;
    }

    @ResolveField('posts', returns => [Post])
    async getPosts(@Parent() author: Author) {
        const { id } = author;
        return await this.postsService.findPostsByAuthorId(id);
    }
}